import cn.hutool.core.lang.Pair;

import java.util.HashMap;
import java.util.Map;

/**
 * 格式计算
 */
public class TaxTest {

    public static void main(String[] name) {
        double totalTax1= calculation(30000,219.12,110,2500);
        System.out.println("目前全年总纳税："+  String.format("%.2f",totalTax1));
        double totalTax2= calculation(25050,219.12,3000,0);
        System.out.println("自缴公积金全年总纳税："+  String.format("%.2f",totalTax2));
        System.out.println("可少交税：" + (totalTax1 - totalTax2));

    }

    /**
     * @param wages 总工资
     * @param social 社保
     * @param fund 公积金
     * @param special 附加专项扣除
     * @return
     */
    private static double calculation(double wages,double social,double fund,double special){
        Map<String, Double> totleMap = new HashMap<>();
        double totalTax=0;
        for (int i = 1; i < 13; i++) {
            double totalWages = totleMap.computeIfAbsent("wages", (k) -> 0.0D) + wages;
            double totalSocial = totleMap.computeIfAbsent("social", (k) -> 0.0D) + social;
            double totalFund = totleMap.computeIfAbsent("fund", (k) -> 0.0D) + fund;
            double totalSpecial = totleMap.computeIfAbsent("special", (k) -> 0.0D) + special;
            double taxAmount = totalWages - totalSocial - totalFund - totalSpecial - 5000 * i ;
            Pair<Double, Integer> pair = getTaxForm(taxAmount);
            Double tax = taxAmount * pair.getKey() - pair.getValue() - totalTax;
            System.out.println("第 "+i+" 个月交税："+  String.format("%.2f",tax));
            totalTax += tax;
            totleMap.put("wages", totalWages);
            totleMap.put("social", totalSocial);
            totleMap.put("fund", totalFund);
            totleMap.put("special", totalSpecial);
        }
        return totalTax;
    }

    private static Pair<Double,Integer> getTaxForm(double value){
        if (value < 36000) {
            return new Pair(0.03,0);
        } else if (value > 36000 && value < 144000) {
            return new Pair(0.1, 2520);
        }else if(value > 144000 && value < 300000){
            return new Pair(0.2, 16920);
        }else if(value > 300000 && value < 420000){
            return new Pair(0.25, 31920);
        }else if(value > 420000 && value < 660000){
            return new Pair(0.3, 52920);
        }else if(value > 660000 && value < 960000){
            return new Pair(0.35, 85920);
        }else{
            return new Pair(0.45, 181920);
        }
    }


}
